﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-12-13 12:49:24
// 负责人：lvsc
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq.Expressions;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
namespace OF.Component.DAL
{

    /// <summary>
    ///快递费用模板 数据访问层
    /// </summary>	
    internal partial class ExpressFeeTpl
    {

        /// <summary>
        /// 根据断言条件获取单个快递费用模板
        /// </summary>
        public ExpressFeeTplInfo QuerySingle(Expression<Func<ExpressFeeTplInfo, bool>> predicate)
        {
            return SQLServerProvider.CreateObject(ConnectionStringKey, predicate);
        }

        /// <summary>
        /// 根据断言条件获获取快递费用模板列表
        /// </summary>
        public IList<ExpressFeeTplInfo> QueryList(Expression<Func<ExpressFeeTplInfo, bool>> predicate)
        {
            return SQLServerProvider.CreateList(ConnectionStringKey, predicate);
        }

        /// <summary>
        /// 获取快递费用
        /// </summary>
        /// <param name="cityId"></param>
        /// <param name="bizTypeId"></param>
        /// <param name="companyId"></param>
        /// <returns></returns>
        public DataTable GetExpressFee(int cityId, int bizTypeId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT T1.ExpressFeeTplID,T1.ExpressCompanyID,T1.ExpressFee,T3.CompanyName,T3.Logo FROM  ExpressFeeTpl T1 WITH(NOLOCK) ");
            strSql.Append("INNER JOIN ExpressFeeTplCity T2 WITH(NOLOCK) ON T1.ExpressFeeTplID=T2.ExpressFeeTplID  ");
            strSql.Append("INNER JOIN ExpressCompany T3 WITH(NOLOCK) ON T1.ExpressCompanyID=T3.ExpressCompanyID ");
            strSql.Append("WHERE T1.BizTypeID=@BizTypeID AND T1.IsActive=1 AND T2.CityID=@CityID ");

            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbc, "@BizTypeID", DbType.Int32, bizTypeId);
            db.AddInParameter(dbc, "@CityID", DbType.Int32, cityId);
            DataSet ds = db.ExecuteDataSet(dbc);
            return ds.Tables[0];
        }


        /// <summary>
        /// 根据城市和公司获取快递费用
        /// </summary>
        /// <param name="companyID"></param>
        /// <param name="cityId"></param>
        /// <param name="bizTypeId"></param>
        /// <returns></returns>
        public ExpressFeeTplInfo GetByCompany(int companyID, int cityId,int bizTypeId)
        {
            Model.ExpressFeeTplInfo info = null;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  T.* FROM    ExpressFeeTpl T WITH ( NOLOCK ) ");
            strSql.Append("INNER JOIN ExpressFeeTplCity T1 WITH ( NOLOCK ) ON T.ExpressFeeTplID = T1.ExpressFeeTplID  ");
            strSql.Append("WHERE T.BizTypeID=@BizTypeID AND T1.CityID=@CityID AND T.ExpressCompanyID=@ExpressCompanyID ");

            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbc, "@BizTypeID", DbType.Int32, bizTypeId);
            db.AddInParameter(dbc, "@CityID", DbType.Int32, cityId);
            db.AddInParameter(dbc, "@ExpressCompanyID", DbType.Int32, companyID);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
        }

        
    }

}